/*     */ package com.zimbra.cs.index.query;
/*     */ 
/*     */ import com.google.common.base.CharMatcher;
/*     */ import com.google.common.base.Joiner;
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.index.LuceneQueryOperation;
/*     */ import com.zimbra.cs.index.LuceneQueryOperation.LazyMultiPhraseQuery;
/*     */ import com.zimbra.cs.index.NoTermQueryOperation;
/*     */ import com.zimbra.cs.index.QueryOperation;
/*     */ import com.zimbra.cs.index.analysis.AddrCharTokenizer;
/*     */ import com.zimbra.cs.index.analysis.ContactTokenFilter;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import java.io.IOException;
/*     */ import java.io.StringReader;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import org.apache.lucene.analysis.TokenStream;
/*     */ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
/*     */ import org.apache.lucene.index.Term;
/*     */ import org.apache.lucene.search.PrefixQuery;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ContactQuery
/*     */   extends Query
/*     */ {
/*  48 */   private final List<String> tokens = new ArrayList();
/*     */   
/*     */   public ContactQuery(String text) {
/*  51 */     TokenStream stream = new ContactTokenFilter(new AddrCharTokenizer(new StringReader(text)));
/*  52 */     CharTermAttribute termAttr = (CharTermAttribute)stream.addAttribute(CharTermAttribute.class);
/*     */     try {
/*  54 */       stream.reset();
/*  55 */       while (stream.incrementToken()) {
/*  56 */         this.tokens.add(CharMatcher.is('*').trimTrailingFrom(termAttr));
/*     */       }
/*  58 */       stream.end();
/*  59 */       stream.close();
/*     */     } catch (IOException e) {
/*  61 */       ZimbraLog.search.error("Failed to tokenize text=%s", new Object[] { text });
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean hasTextOperation()
/*     */   {
/*  67 */     return true;
/*     */   }
/*     */   
/*     */   public QueryOperation compile(Mailbox mbox, boolean bool) throws ServiceException
/*     */   {
/*  72 */     switch (this.tokens.size()) {
/*     */     case 0: 
/*  74 */       return new NoTermQueryOperation();
/*     */     case 1: 
/*  76 */       LuceneQueryOperation op = new LuceneQueryOperation();
/*  77 */       PrefixQuery query = new PrefixQuery(new Term("l.contactData", (String)this.tokens.get(0)));
/*  78 */       op.addClause("contact:" + (String)this.tokens.get(0), query, evalBool(bool));
/*  79 */       return op;
/*     */     }
/*     */     
/*  82 */     LuceneQueryOperation op = new LuceneQueryOperation();
/*  83 */     LuceneQueryOperation.LazyMultiPhraseQuery query = new LuceneQueryOperation.LazyMultiPhraseQuery();
/*  84 */     for (String token : this.tokens) {
/*  85 */       query.expand(new Term("l.contactData", token));
/*     */     }
/*  87 */     op.addClause("contact:\"" + Joiner.on(' ').join(this.tokens) + "\"", query, evalBool(bool));
/*  88 */     return op;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   void dump(StringBuilder out)
/*     */   {
/*  95 */     out.append("CONTACT:");
/*  96 */     Joiner.on(',').appendTo(out, this.tokens);
/*     */   }
/*     */   
/*     */   void sanitizedDump(StringBuilder out)
/*     */   {
/* 101 */     out.append("CONTACT:");
/* 102 */     out.append(Strings.repeat("$TEXT,", this.tokens.size()));
/* 103 */     if (out.charAt(out.length() - 1) == ',') {
/* 104 */       out.deleteCharAt(out.length() - 1);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/query/ContactQuery.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */